import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Query,
  UseInterceptors,
  UploadedFiles,
  Inject,
  OnModuleInit,
  OnApplicationBootstrap,
  OnModuleDestroy,
  BeforeApplicationShutdown,
  OnApplicationShutdown,
  ValidationPipe,
  HttpException,
  HttpStatus,
  BadRequestException,
  Version,
} from '@nestjs/common';
import { PersonService } from './person.service';
import { CreatePersonDto } from './dto/create-person.dto';
import { UpdatePersonDto } from './dto/update-person.dto';
import { AnyFilesInterceptor } from '@nestjs/platform-express';
import { AppService } from 'src/app.service';

@Controller({
  path: 'api/person',
  version: '2',
})
export class Person2Controller
  implements
    OnModuleInit,
    OnApplicationBootstrap,
    OnModuleDestroy,
    BeforeApplicationShutdown,
    OnApplicationShutdown
{
  constructor(private readonly personService: PersonService) {}

  @Inject('person2')
  private readonly person2: {
    name: string;
    age: string;
  };

  // @Inject(AppService)
  // private readonly appService: AppService;

  @Inject('CONFIG_OPTIONS')
  private readonly configOptions: Record<string, any>;

  onModuleInit() {
    console.log('PersonController onModuleInit~');
  }

  onApplicationBootstrap() {
    console.log('PersonController onApplicationBootstrap~');
  }
  onModuleDestroy() {
    console.log('PersonService onModuleDestroy~');
  }

  beforeApplicationShutdown(signal: string) {
    console.log('PersonService beforeApplicationShutdown~', signal);
  }
  onApplicationShutdown() {
    console.log('PersonService onApplicationShutdown~');
  }
  @Get('find')
  query(@Query('name') name: string, @Query('age') age: number) {
    console.log('handle...');

    return `received2: name=${name},age=${age}`; // ${this.appService.getHello()}
  }

  // @Get(':id')
  // urlParam(@Param('id') id: string) {
  //   return `received id: ${id}`;
  //   // return this.personService.findOne(+id);
  // }

  @Post()
  body(@Body(new ValidationPipe()) createPersonDto: CreatePersonDto) {
    console.log('body2的值是===', createPersonDto);
    return `返回值2==${JSON.stringify(createPersonDto)}`;
  }

  @Get('/err')
  a(@Query('id') id: string) {
    // 自定义错误信息
    throw new BadRequestException('Forbidden123');
    throw new HttpException('Forbidden123', HttpStatus.BAD_REQUEST);
    // return `received id: ${id}`;
    // return this.personService.findOne(+id);
  }

  @Post('file')
  @UseInterceptors(
    AnyFilesInterceptor({
      dest: 'uploads/',
    }),
  )
  body2(
    @Body() createPersonDto: CreatePersonDto,
    @UploadedFiles() files: Array<Express.Multer.File>,
  ) {
    return `received2: ${JSON.stringify(createPersonDto)}`;
  }
}
